package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public int save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //删除Redis中原有的分类列表
        removeRedis();
        return categoryMapper.save(category);
    }

    @Override
    public PageResult getPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Category category = new Category();
        BeanUtils.copyProperties(categoryPageQueryDTO, category);
        List<Category> list = categoryMapper.getPage(category);
        Page<Category> page = (Page) list;
        Long total = page.getTotal();
        List<Category> pageList = page.getResult();
        return new PageResult(total, pageList);
    }

    @Override
    public int deleteById(Long id) {
        //删除Redis中原有的分类列表
        removeRedis();
        return categoryMapper.deleteById(id);
    }

    @Override
    public int update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //删除Redis中原有的分类列表
        removeRedis();
        return categoryMapper.update(category);
    }

    @Override
    public List<Category> getList(Integer type) {
        List<Category> list = null;
        if(type==null){
            list = categoryMapper.getCategoryList();
            return list;
        }
        String s = (String) redisTemplate.opsForHash().get("category_list:", type.toString());
        if (!StringUtils.isBlank(s)) {
            list = JSON.parseArray(s, Category.class);
        }
        if(CollectionUtils.isEmpty(list)){
            list = categoryMapper.getList(type);
            String jsonString = JSON.toJSONString(list);
            redisTemplate.opsForHash().put("category_list:", type.toString(), jsonString);
        }
        return list;
    }

    public void removeRedis(){
        redisTemplate.delete("category_list:");
    }


}
